//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "BindingSitePredictionData.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define MAX_SIZE 500000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
BindingSitePredictionData::BindingSitePredictionData () {

  this->filenameIn                           = new String ();
  this->listPredictionBS                     = new TListE <PredictBindingSiteByPattern> ();
  this->toString                             = new String ();

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
BindingSitePredictionData::BindingSitePredictionData (const BindingSitePredictionData& bindingSitePredictionData) {

  this->filenameIn                           = new String (bindingSitePredictionData.filenameIn);
  this->listPredictionBS                     = new TListE <PredictBindingSiteByPattern> (bindingSitePredictionData.listPredictionBS);
  this->toString                             = new String (bindingSitePredictionData.toString);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
BindingSitePredictionData::BindingSitePredictionData (const BindingSitePredictionData* bindingSitePredictionData) {

  this->filenameIn                           = new String (bindingSitePredictionData->filenameIn);
  this->listPredictionBS                     = new TListE <PredictBindingSiteByPattern> (bindingSitePredictionData->listPredictionBS);
  this->toString                             = new String (bindingSitePredictionData->toString);

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
BindingSitePredictionData::~BindingSitePredictionData () {

  if (this->filenameIn)       delete this->filenameIn;
  if (this->listPredictionBS) delete this->listPredictionBS;
  if (this->toString)         delete this->toString;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void BindingSitePredictionData::FilenameIn (String* filenameIn) {

  if (filenameIn)
    *(this->filenameIn) = *filenameIn;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void BindingSitePredictionData::ListPredictionBS (TListE<PredictBindingSiteByPattern>* listPredictionBS) {

  if (listPredictionBS)
    *(this->listPredictionBS) = *listPredictionBS;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* BindingSitePredictionData::FilenameIn (void) {

  return this->filenameIn;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE<PredictBindingSiteByPattern>* BindingSitePredictionData::ListPredictionBS (void) {

  return this->listPredictionBS;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool BindingSitePredictionData::LoadData (void) {

  if (!this->filenameIn || !this->filenameIn->Out())
    return false;
  
  ifstream file (this->filenameIn->Out());
  
  if (!file.is_open())
    return false;
  
  char    buffer [MAX_SIZE];
  String* data = new String ("");
  
  while (!file.eof()) {
    
    file.getline(buffer, MAX_SIZE);
    
    if (buffer[0] == '>') {
      
      if (data->Length()) {
        
        PredictBindingSiteByPattern* newPredictBindingSiteByPattern = new PredictBindingSiteByPattern ();
        
        if (newPredictBindingSiteByPattern->LoadDataFromString(data))
          this->listPredictionBS->Add(newPredictBindingSiteByPattern);
        else
          delete newPredictBindingSiteByPattern;
        
      }
      
      data->In(buffer);
      data->Add("\n");
      
    }
    else {
      data->Add(buffer);
      data->Add("\n");
    }
    
  }
  
  if (data->Length()) {

    PredictBindingSiteByPattern* newPredictBindingSiteByPattern = new PredictBindingSiteByPattern ();

    if (newPredictBindingSiteByPattern->LoadDataFromString(data))
      this->listPredictionBS->Add(newPredictBindingSiteByPattern);
    else
      delete newPredictBindingSiteByPattern;

  }

  file.close();
  
  if (data) delete data;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool BindingSitePredictionData::CleanPredictions (int value) {
  
  if (!this->listPredictionBS || !this->listPredictionBS->Length())
    return false;
  
  this->listPredictionBS->SetInitial();
  while (this->listPredictionBS->SetNext()) {
    
    PredictBindingSiteByPattern* currentPredictBindingSiteByPattern = this->listPredictionBS->GetCurrent();
    
    for (int i = 0; i < currentPredictBindingSiteByPattern->Sequence()->Length(); i++) {
      ((currentPredictBindingSiteByPattern->ScoreAA())[i])    = value;
      ((currentPredictBindingSiteByPattern->ScoreCH())[i])    = value;
      ((currentPredictBindingSiteByPattern->ScorePH())[i])    = value;
      ((currentPredictBindingSiteByPattern->ScoreSS())[i])    = value;
      ((currentPredictBindingSiteByPattern->ScoreMT())[i])    = value;
      ((currentPredictBindingSiteByPattern->ScoreCumul())[i]) = value;
    }
    
  }
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
BindingSitePredictionData BindingSitePredictionData::operator= (const BindingSitePredictionData& bindingSitePredictionData) {
cout << "A" << endl;
  *(this->filenameIn)       = *(bindingSitePredictionData.filenameIn);
  *(this->listPredictionBS) = *(bindingSitePredictionData.listPredictionBS);
cout << "B" << endl;

  return *this;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
